package com.projectx.action.draw;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.flask.colorpicker.builder.ColorPickerClickListener;
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
import com.google.android.material.appbar.MaterialToolbar;
import com.projectx.R;
import com.projectx.action.chat.ChatSettingActivity;

import java.util.zip.Inflater;

public class DrawBoardActivity extends AppCompatActivity {
    private Button paintStoke;
    private Button paintColor;
    private Button paintSave;
    private Button back;
    private MaterialToolbar materialToolbar;
    private DrawBoardView drawBoard;
    private TextView progressText;
    private Paint paint;
    private SeekBar seekBar;
    private float cacheStroke;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw_board);

        paintColor = findViewById(R.id.paint_color);
        paintStoke = findViewById(R.id.paint_stoke);
        paintSave = findViewById(R.id.paint_save);

        back = findViewById(R.id.back);
        materialToolbar = findViewById(R.id.paint_board_bar);
        drawBoard = findViewById(R.id.paint_board);
        paint = drawBoard.initPaint();

        try {
            getSupportActionBar().hide(); //去除标题栏
        } catch (NullPointerException e){
            Log.d("KMH","没有状态栏标题");
        }

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        materialToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.paint_undo:
                        if(!drawBoard.undo()){
                            Toast.makeText(DrawBoardActivity.this,"撤销失败",Toast.LENGTH_SHORT).show();
                        }
                        return true;
                    case R.id.paint_redo:
                        if(!drawBoard.redo()){
                            Toast.makeText(DrawBoardActivity.this,"反撤销失败",Toast.LENGTH_SHORT).show();
                        }
                        return true;
                    case R.id.paint_clear:
                        AlertDialog.Builder refreshDialog = new AlertDialog.Builder(DrawBoardActivity.this);
                        refreshDialog.setTitle("清除画板").setMessage("是否清除画板")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        drawBoard.clear();
                                    }
                                }).setNegativeButton("取消",null).show();
                        return true;
                    case R.id.paint_eraser:
                        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                        drawBoard.setEditMode(EditMode.EDIT_MODE_ERASER);
                        return true;
                    case R.id.paint_create:
                        paint.setXfermode(null);
                        drawBoard.setEditMode(EditMode.EDIT_MODE_PAN);
                        return true;
                }
                return false;
            }
        });

        paintColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ColorPickerDialogBuilder.with(DrawBoardActivity.this).setTitle("设置线条颜色").initialColor(paint.getColor())
                        .setPositiveButton("确定", new ColorPickerClickListener() {
                            @Override
                            public void onClick(DialogInterface d, int lastSelectedColor, Integer[] allColors) {
                                paint.setColor(lastSelectedColor);
                                drawBoard.setPaint(paint);
                            }
                        })
                        .setNegativeButton("取消",null).build().show();
            }
        });

        paintStoke.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string;
                string = "线条宽度: " + ((int)paint.getStrokeWidth() + 1) + "/50";
                View layout = LayoutInflater.from(DrawBoardActivity.this).inflate(R.layout.item_seek_bar,null);
                progressText = layout.findViewById(R.id.seek_bar_progress);
                seekBar = layout.findViewById(R.id.paint_stoke_seek_bar);
                cacheStroke = paint.getStrokeWidth();
                progressText.setText(string);
                seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        String str = "线条宽度: " + (progress + 1) + "/50";
                        progressText.setText(str);
                        cacheStroke = progress;
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {

                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {

                    }
                });
                seekBar.setProgress((int) paint.getStrokeWidth());
                AlertDialog.Builder stokePicker = new AlertDialog.Builder(DrawBoardActivity.this);
                stokePicker.setView(layout);
                stokePicker.setTitle("设置线条宽度");
                stokePicker.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        paint.setStrokeWidth(cacheStroke);
                        drawBoard.setPaint(paint);
                    }
                });
                stokePicker.setNegativeButton("取消",null);
                stokePicker.show();
            }
        });

        paintSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (drawBoard.save()){
                    Toast.makeText(DrawBoardActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(DrawBoardActivity.this,"保存失败",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    @Override
    public void onBackPressed() {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("确定退出").setMessage("是否退出画图板，您的所有更改可能会丢失")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setNegativeButton("取消",null).show();
    }
}
